package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程
 * </p>
 *
 * @author testjava
 * @since 2021-03-09
 */
@Api(tags = "课程基本信息")
@RestController
@RequestMapping("/eduservice/edu-course")
//@CrossOrigin  //解决跨域
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;


    //课程列表 基本实现
    //TODO 待完成 条件查询 分页
    @GetMapping("/list")
    public R list(){
        List<EduCourse> courseList = courseService.list(null);
        System.out.println("courseList"+courseList);
        return R.ok().data("list",courseList);

    }


    /**
     *   添加课程基本信息
     * @param courseInfoVo
     * @return  新加课程的id值
     */
    @ApiOperation(value = "添加课程基本信息")
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){

        String id=courseService.saveCourseInfo(courseInfoVo);

        return R.ok().data("courseId",id);
    }

    //根据课程id 查询课程的基本信息  做数据回显
    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo=courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    //修改课程信息
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){

        courseService.updateCourseInfo(courseInfoVo);

        return R.ok();
    }
    //根据课程id  查询基本信息 确认发布
    @GetMapping("/getPublishCourseInfo/{id}")
    public R  getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo= courseService.publishCourseInfo(id);
        System.out.println("coursePublishVo"+coursePublishVo);
        return R.ok().data("publishCourse",coursePublishVo);
    }
    //课程最终发布
    @PostMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse course = new EduCourse();
        course.setId(id);
        course.setStatus("Normal");
        courseService.updateById(course);
        return  R.ok();
    }

    /**
     *   删除课程  课程表 课程描述表 章节表 小节表
     * @param courseId  课程Id
     * @return
     */
    @DeleteMapping("/delete/{courseId}")
    public R delete(@PathVariable String courseId){
        courseService.removeCourse( courseId);
         return R.ok();
    }
}

